<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ include file="../../common/layui.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<head>
    <title>部门管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
</head>
<body>

<!-- layui tree 的方式 -->


<div class="section_mian">
    <!-- 列表页 -->
    <div class="larry-personal">
        <div class="layui-tab">
            <!-- 功能区域 -->
            <blockquote class="layui-elem-quote news_search">
                <div class="layui-inline"><a class="layui-btn btn_color_lanse" onclick="addOrUpdate('add','','')" id="resource_tjzy">添加一级部门</a></div>
            </blockquote>
            <!-- 功能区域结束 -->
            <div class="resource_shu_main">
                <div id="showhtml" style="padding-left: 20px;"></div>
            </div>

            <script type="text/javascript" src="/center/js/jquery.tree.js"></script>
            <script type="text/javascript">

                function loadTreeData() {//初始化加载 tree
                    var nodes=[];
                    $.ajax({
                        url:"depart/tree",
                        type:"post",
                        cache:false,
                        async:false,
                        //data:{"parentId":parentId},
                        dataType:"json",
                        success:function(data){
                            nodes = data.model;
                            return nodes;
                        },
                        error:function(data){
                            console.log("error:");
                            console.log(data)
                        }
                    });
                    return nodes;
                }



                function initFirst() {//初始化加载 //以后的级别 多了要用递归，但是递归的循环次数，远比这个多
                    var nodes = loadTreeData();
                    if(nodes){
                        $("#showhtml").html(nodes);
                    }
                    var addHTml = "";
                    $("#shu_files").append(addHTml);
                }

                $(function(){
                    //一次加载
                    initFirst();//初始化一级菜单
                    $('#shu').tree({ });//渲染tree
                    renderForm();
                    $('#shu_files').tree({ });//渲染tree
                });

                function renderForm(){
                    layui.use([ 'form' ], function(){
                        var form = layui.form;
                        form.render();//重新渲染
                    });
                }

            </script>
        </div>
    </div>
    <!-- 列表页结束 -->




    <script>

        //查看、添加、修改 的公用页面
        function addOrUpdate(load,id,parentId) {
            var titles = "资源信息";
            if(load=='add'){//添加
                titles = "添加资源";
            }else if(load=='detail'){//查看
                titles = "查看资源";
            }else if(load=='update'){//修改
                titles = "修改资源";
            }
            parent.layer.open({
                type:2,
                btn: ['确定', '取消'],
                area: ['40%', '60%'],
                skin: 'layui-layer-lan',
                title: titles,
                shade: [0.5, '#393D49'],
                maxmin: true, //开启最大化最小化按钮
                content:"depart/load?load="+load+"&id="+id+"&parentId="+parentId,
                yes:function(index,frameDom){
                    var openFrame = $(frameDom).find("iframe")[0].contentWindow; // 窗口
                    if(load=='add'){//添加
                        if(openFrame.saveOrUpdate()){//调用 添加方法
                            parent.layer.close(index);//关闭当前窗口
                            reloadTree();



                        }else{
                            return false;
                        }
                    }else if(load=='detail'){//查看
                        parent.layer.close(index);//关闭当前窗口
                    }else if(load=='update'){//修改
                        openFrame.saveOrUpdate();//调用 添加方法
                        parent.layer.close(index);//关闭当前窗口
                        setTimeout(reloadTree,500);
                    }
                }

            });
        }

        function  reloadTree() {//刷新tree
            location.href = "depart/index"
        }


        function  addNode(parentId) {
            addOrUpdate('add','',parentId)
        }

        function updateNode(id){
            addOrUpdate('update',id,'')
        }

        function delNode(id){

            parent.layer.confirm('确认要删除吗？', {
                btn : [ '确定', '取消' ]//按钮
            }, function(index) {
                //执行删除……
                var url = "depart/delete?id="+id;
                $.ajax({
                    url:url,
                    type:"POST",
                    data:{},
                    dataType:"json",
                    success:function(data){
                        parent.layer.msg('删除成功！', {icon: 1});
                        $("#"+id).remove();
                    },
                    error:function(data){
                        parent.layer.msg('系统异常！', {icon: 5});
                    }
                });


            });


        }
    </script>
</div>






</body>
</html>
